2020-2021 Sunseeker Telemetry and Lighting System
usci_b_i2c_ex2_masterTxMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 //*****************************************************************************
49 //
50 //*****************************************************************************
51 //*****************************************************************************
52 //
53 //Set the address for slave module. This is a 7-bit address sent in the
54 //following format:
55 //[A6:A5:A4:A3:A2:A1:A0:RS]
56 //
57 //A zero in the "RS" position of the first byte means that the master
58 //transmits (sends) data to the selected slave, and a one in this position
59 //means that the master receives data from the slave.
60 //
61 //*****************************************************************************
62 #define SLAVE_ADDRESS 0x48
63 //*****************************************************************************
64 //
65 //Specify number of bytes to be transmitted
66 //
67 //*****************************************************************************
68 #define TXLENGTH 0x04
69 
70 uint8_t transmitData[40] = { 0x61,
71  0x62,
72  0x63,
73  0x64,
74  0x65,
75  0x66,
76  0x67,
77  0x68};
78 
79 uint8_t transmitCounter = 0;
80 
81 void main ()
82 {
83  //Stop WDT
84  WDT_A_hold(WDT_A_BASE);
85 
86  //Assign I2C pins to USCI_B0
87  GPIO_setAsPeripheralModuleFunctionInputPin(
88  GPIO_PORT_P3,
89  GPIO_PIN1 + GPIO_PIN2
90  );
91 
92  //Initialize Master
93  USCI_B_I2C_initMasterParam param = {0};
94  param.selectClockSource = USCI_B_I2C_CLOCKSOURCE_SMCLK;
95  param.i2cClk = UCS_getSMCLK();
96  param.dataRate = USCI_B_I2C_SET_DATA_RATE_400KBPS;
97  USCI_B_I2C_initMaster(USCI_B0_BASE, &param);
98 
99  //Specify slave address
100  USCI_B_I2C_setSlaveAddress(USCI_B0_BASE,
102  );
103 
104  //Set Transmit mode
105  USCI_B_I2C_setMode(USCI_B0_BASE,
106  USCI_B_I2C_TRANSMIT_MODE
107  );
108 
109  //Enable I2C Module to start operations
110  USCI_B_I2C_enable(USCI_B0_BASE);
111 
112 
113  while (1)
114  {
115  //Enable transmit Interrupt
116  USCI_B_I2C_clearInterrupt(USCI_B0_BASE,
117  USCI_B_I2C_TRANSMIT_INTERRUPT
118  );
119  USCI_B_I2C_enableInterrupt(USCI_B0_BASE,
120  USCI_B_I2C_TRANSMIT_INTERRUPT
121  );
122 
123  //Delay between each transaction
124  __delay_cycles(50);
125 
126  //Load TX byte counter
127  transmitCounter = 1;
128 
129  //Initiate start and send first character
130  USCI_B_I2C_masterSendMultiByteStart(USCI_B0_BASE,
131  transmitData[0]
132  );
133 
134  //Enter LPM0 with interrupts enabled
135  __bis_SR_register(LPM0_bits + GIE);
136  __no_operation();
137 
138  //Delay until transmission completes
139  while (USCI_B_I2C_isBusBusy(USCI_B0_BASE)) ;
140  }
141 }
142 
143 //******************************************************************************
144 //
145 //This is the USCI_B0 interrupt vector service routine.
146 //
147 //******************************************************************************
148 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
149 #pragma vector=USCI_B0_VECTOR
150 __interrupt
151 #elif defined(__GNUC__)
152 __attribute__((interrupt(USCI_B0_VECTOR)))
153 #endif
154 void USCI_B0_ISR (void)
155 {
156  switch (__even_in_range(UCB0IV,12)){
157  case USCI_I2C_UCTXIFG:
158  {
159  //Check TX byte counter
160  if (transmitCounter < TXLENGTH){
161  //Initiate send of character from Master to Slave
162  USCI_B_I2C_masterSendMultiByteNext(USCI_B0_BASE,
164  );
165 
166  //Increment TX byte counter
167  transmitCounter++;
168  } else {
169  //Initiate stop only
170  USCI_B_I2C_masterSendMultiByteStop(USCI_B0_BASE);
171 
172  //Clear master interrupt status
173  USCI_B_I2C_clearInterrupt(USCI_B0_BASE,
174  USCI_B_I2C_TRANSMIT_INTERRUPT);
175 
176  //Exit LPM0 on interrupt return
177  __bic_SR_register_on_exit(LPM0_bits);
178  }
179  break;
180  }
181  }
182 }
183 
MPU_initThreeSegmentsParam param
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)
uint8_t transmitData[40]
uint8_t transmitCounter
void USCI_B0_ISR(void)
#define SLAVE_ADDRESS